#include "clock.h"

ClockElement::ClockElement(){ }

SignalContainer& ClockElement::getSignalCollection()
{
    return voidContainer;
}

bool ClockElement::getEditable()
{
    return false;
}

void ClockElement::generateSignal(uint time, uint tact)
{
    uint timex = scheme->ticPerTact*tact + time;
    if (timex < shift)
        pins.at(0)->send(1 - signal);
    else
    {
        timex = (timex - shift) % period;
        if ((timex >= startTime) && (timex < clockTime + startTime))
            pins.at(0)->send(signal);
        else
            pins.at(0)->send(1 - signal);
    }
}

void ClockElement::initData(const QDomNode &additionals)
{
    startTime = (additionals.namedItem("starttime").nodeValue()).toInt();
    period = (additionals.namedItem("period").nodeValue()).toInt();
    clockTime = (additionals.namedItem("clocktime").nodeValue()).toInt();
    signal = (additionals.namedItem("signal").nodeValue()).toInt();
    shift = (additionals.namedItem("shift").nodeValue()).toInt();
}

void ClockElement::saveData(QDomNode &additionals)
{
    XmlHelper::addSimpleXmlNode(additionals, "starttime", QString::number(startTime));
    XmlHelper::addSimpleXmlNode(additionals, "clocktime", QString::number(clockTime));
    XmlHelper::addSimpleXmlNode(additionals, "period", QString::number(period));
    XmlHelper::addSimpleXmlNode(additionals, "signal", QString::number(signal));
    XmlHelper::addSimpleXmlNode(additionals, "shift", QString::number(shift));
}
